package com.ryan.ohos.extension.widget;


import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

public class Toast {


    public enum ToastLayout {
        DEFAULT,
        CENTER,
        TOP,
        BOTTOM,
    }


    public static void show(Context mContext, String content) {
        createToast(mContext, content, ToastLayout.BOTTOM);
    }


    public static void show(Context mContext, String content, ToastLayout layout) {
        createToast(mContext, content, layout);
    }


    public static void show(Context mContext, int content) {
        createToast(mContext, getString(mContext, content), ToastLayout.BOTTOM);
    }


    public static void show(Context mContext, int content, ToastLayout layout) {
        createToast(mContext, getString(mContext, content), layout);
    }

    private static void createToast(Context mContext, String content, ToastLayout layout) {
        DirectionalLayout toastLayout = new DirectionalLayout(mContext);
        DirectionalLayout.LayoutConfig textConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        Text text = new Text(mContext);
        text.setText(content);
        text.setMultipleLine(true);
        text.setTextColor(new Color(Color.getIntColor("#ffffff")));
        text.setPadding(AttrHelper.vp2px(16, mContext), AttrHelper.vp2px(4, mContext), AttrHelper.vp2px(16, mContext), AttrHelper.vp2px(4, mContext));
        text.setTextSize(AttrHelper.fp2px(14, mContext));
        text.setBackground(buildDrawableByColorRadius(Color.getIntColor("#70000000"), AttrHelper.vp2px(16, mContext)));
        text.setLayoutConfig(textConfig);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setMarginLeft(AttrHelper.vp2px(30, mContext));
        text.setMarginRight(AttrHelper.vp2px(30, mContext));
        int mLayout = LayoutAlignment.CENTER;
        switch (layout) {
            case TOP:
                text.setMarginTop(AttrHelper.vp2px(30, mContext));
                mLayout = LayoutAlignment.TOP;
                break;
            case BOTTOM:
                mLayout = LayoutAlignment.BOTTOM;
                text.setMarginBottom(AttrHelper.vp2px(30, mContext));
                break;
            case CENTER:
                mLayout = LayoutAlignment.CENTER;
                break;
        }
        toastLayout.addComponent(text);
        ToastDialog toastDialog = new ToastDialog(mContext);
        toastDialog.setComponent(toastLayout);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(mLayout);
        toastDialog.setTransparent(true);
        toastDialog.show();
    }


    private static ohos.agp.components.element.Element buildDrawableByColorRadius(int color, float radius) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(0);
        drawable.setRgbColor(RgbColor.fromArgbInt(color));
        drawable.setCornerRadius(radius);
        return drawable;
    }

    private static String getString(Context mContent, int resId) {
        try {
            return mContent.getResourceManager().getElement(resId).getString();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        return "";
    }

}
